package icu.groot.alan.servlet.announce;

import com.fasterxml.jackson.databind.ObjectMapper;
import icu.groot.alan.entity.TokenEntity;
import icu.groot.alan.service.AnnounceService;
import icu.groot.alan.service.impl.AnnounceServiceImpl;
import icu.groot.alan.util.R;
import org.apache.commons.lang3.StringUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/announce/announce.do")
public class AnnounceServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String taskCode = req.getParameter("taskCode");
        String endTime = req.getParameter("endTime");
        TokenEntity tokenEntity = (TokenEntity) req.getAttribute("tokenEntity");
        String code = tokenEntity.getCode();
        ObjectMapper objectMapper = new ObjectMapper();

        if (StringUtils.isAnyBlank(taskCode, endTime, code)){
            resp.getWriter().print(objectMapper.writeValueAsString(R.error("参数不全")));
            return;
        }
        resp.getWriter().print(objectMapper.writeValueAsString(new AnnounceServiceImpl().announceTask(taskCode, endTime, code)));
    }
}
